// pages/userInfo/userInfo.js
let app = getApp();
import { serverIp, getUserInfo, getSession } from '../../utils/util';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } 
  },

  getUserInfo: function (event) {
    //向服务端发送用户信息，并保存
    wx.request({
      url: serverIp + '/api/weixin/getuserbyencrypteddata/',
      header: { 
        'content-type': 'application/x-www-form-urlencoded',
        'cookie': getSession()
      },
      data: {
        encryptedData: event.detail.encryptedData,
        iv: event.detail.iv
      },
      method: 'POST',
      success: (res) => {
        console.log(res)
        this.setData({
          hasUserInfo: true,
          userInfo: res.data.res
        });
      }
    })
  }
})